package com.liming.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liming.auth.mapper.RegionMapper;
import com.liming.auth.service.RegionService;
import com.liming.common.pojo.entity.authinfo.Region;
import com.liming.common.pojo.vo.authinfo.mini.MiniRegionVO;
import com.liming.common.util.mybatisplus.MPUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liming
 * @date 2025/1/20
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService, MPUtil<Region> {

    @Override
    public Map<Long, String> idAndNameMap() {
        return lambdaQuery().list().stream().collect(Collectors.toMap(Region::getId, Region::getName));
    }

    @Override
    public Map<String, List<MiniRegionVO>> miniRegionMap() {
        return lambdaQuery()
                .list()
                .stream()
                .collect(Collectors.groupingBy(region -> region.getParentId().toString(),
                        Collectors.mapping(region -> {
                            MiniRegionVO vo = new MiniRegionVO();
                            vo.setLabel(region.getName());
                            vo.setValue(region.getId());
                            return vo;
                        }, Collectors.toList())));
    }

}
